import 'dart:developer';
import 'package:get/get.dart';
import 'package:sports_new/entity/ai/ai_pay_entity.dart';
import 'package:sports_new/entity/ai/model_entity.dart';
import 'package:sports_new/entity/pay/recharge_entity.dart';
import 'package:sports_new/utils/toast_utils.dart';
import 'package:sports_new/view/pick/ai/ai_type.dart';
import '../../entity/match/secret_price_entity.dart';
import '../../server/api/ai_api.dart';
import '../../server/api/coupon_api.dart';
import '../../server/api/other_api.dart';
import '../../server/api/pay_api.dart';
import '../../utils/user.dart';
import '../../utils/utils.dart';
import '../../widgets/pay_bottom_sheet.dart';

class SecretPayLogic extends GetxController{
  int _priceIndex = 0;
  int? roomId = Get.parameters['roomId'].toInt();
  String? tip;
  SecretPayEntity? data;
  List<SecretPrices>? price;
  int get priceIndex => _priceIndex;

  set priceIndex(int value) {
    _priceIndex = value;
    update();
  }

  @override
  void onReady() {
    requestData();
    super.onReady();
  }

  Future toPay(SecretPrices price) async{
    Utils.onEvent("hwqb_ljzf");
    double money = price.price ?? 0;
    final coupons = await CouponApi.use1Coupons(
        gold: money,scene: PayScene.secret, otherId: '',roomId: roomId);
    final PayResult? result = await Get.bottomSheet(
      isScrollControlled: true,
      PayBottomSheet(
        payInfo: PayInfo(
          price: money.toString(),
          payScene: PayScene.secret,
          buyId: price.id.toString(),
          coupons: coupons
        )
      )
    );
    if(result != null && result.toPay == PayStatus.goldPay){
      var s = await PayApi.secretOrder(
        price.id,
        couponGold: result.coupon?.gold,
        couponId: result.coupon?.id,
        roomId: roomId
      );
      if(s == true) {
        Utils.onEvent('pay_done');
        Get.back();
      }else{
        Utils.onEvent('jmnc_zfyd_qxzf');
      }
    }else if(result?.toPay == PayStatus.directPay){
      Utils.onEvent('jmnc_zfyd_qrzf');
      Get.back();
    }else{
      Utils.onEvent('jmnc_zfyd_qxzf');
    }
    User.fetchUserInfo(fetchFocus: false);
  }

  Future requestData() async{
    data = await PayApi.getSecretPay(roomId: roomId);
    price = data?.priceList;
    var tips = await OtherApi.getAppList("app_res_tips");
    try {
      tip = tips?.firstWhere((element) => element.button == "secret_pay_tip").content;
      update();
    }catch(e){
      log("no secret_pay");
    }
    update();
  }
}